/**
 * Created by 万林 on 2016/9/14 0014.
 */
var menuItemImageArray = ["lxhHome.gif","lxhCodeLife.gif","lxhMovie.gif","lxhMusic.gif","lxhAnime.gif","lxhAbout.gif"];
var menuItemTexeArray = ["返回首页吧！<br>一切从头开始！","程序人生，CodeLife！<br>一些关于编程的知识总结和分享","电影，<br>人类伟大的发明，<br>这里有我喜欢的一些电影。<br>说不定你也喜欢哦!","有什么烦恼是听一首歌解决不了的呢？<br>如果有，<br>那一定是两首！","罗小黑万岁！<br>没错！<br>这里有我喜欢的动画，包括TV动画和动画电影。","关于我的一些信息<br>想知道？<br>那就点进来吧!"];
var pageArray = ["home.html","codeLife.html","movie.html","music.html","anime.html","about.html"];
var BSKnumber = 100;
var menu =null;
var menuItems = null;
var menuTimeOut = null;
var player = null;
var progresRang = null;

window.onload = function () {
    setTimeout(headImageAnimation,1000);
    var headImg = document.getElementById("headImg");
    menu = getElementsByClassName(document.body,"menuDiv")[0];
    menuItems = getElementsByClassName(menu,"menuItem");
    player = document.getElementById("player");
    progresRang = document.getElementById("progresRang");
    menuItems[0].selected = true;
    menuItems[0].style.backgroundColor = "rgba(70, 192, 220,1)";
    doSomeThing();
    menu.onmouseout = function () {
       closeMenu();
    };
    menu.onmouseover= function () {
        clearTimeout(menuTimeOut);
    };
    for (var i = 0;i<menuItems.length;i++){
        menuItems[i].index = i+BSKnumber;
        if (menuItems[i].selected){
            menuItems[i].alpha = 1;
        }else {
            menuItems[i].alpha = 0.4;
        }
        menuItems[i].onmouseover = function () {
            var menuItem = this;
            var menuImage = document.getElementById("menuContentImage");
            var menuText = document.getElementById("menuContentText");
            menuImage.src = "../image/"+menuItemImageArray[menuItem.index-BSKnumber];
            menuText.innerHTML = menuItemTexeArray[menuItem.index-BSKnumber];
            openMenu();
            clearInterval(menuItem.timer2);
            menuItem.timer2 = setInterval(function () {
                menuItem.alpha +=0.2;
                if(menuItem.alpha >= 1){
                    menuItem.alpha = 1;
                    clearInterval(menuItem.timer2);
                }
                menuItem.style.backgroundColor = " rgba(70, 192, 220,"+menuItem.alpha+")";
            },30);
        };
        menuItems[i].onmouseout = function () {
            var menuItem = this;
            if(menuItem.selected){
                return;
            }
            clearInterval(menuItem.timer2);
            menuItem.timer2 = setInterval(function () {
                menuItem.alpha -=0.05;
                if(menuItem.alpha <= 0.4){
                    menuItem.alpha = 0.4;
                    clearInterval(menuItem.timer2);
                }
                menuItem.style.backgroundColor = " rgba(70, 192, 220,"+menuItem.alpha+")";
            },30);
        };
        menuItems[i].onclick = function () {
            if (this.selected){
                return;
            }

            var aIframe = document.getElementById("contentIFrame");
            aIframe.src = pageArray[this.index-BSKnumber];
            if (this.index==BSKnumber){
                var leftValue1 = parseInt(document.body.offsetWidth/2-100);
                AnimatedObjEasyinout(headImg,{width:200,height:200,left:leftValue1,top:200},function () {
                    headImg.style.left = (leftValue1/document.body.offsetWidth)*100+"%";
                });
            }else if(menuItems[0].selected){
                AnimatedObjEasyinout(headImg,{width:100,height:100,left:20,top:10});
            }
            for(var j = 0;j<menuItems.length;j++){
                if (menuItems[j].selected){
                    var aaitem = menuItems[j];
                    clearInterval(aaitem.timer2);
                    aaitem.timer2 = setInterval(function () {
                        aaitem.alpha -=0.05;
                        if(aaitem.alpha <= 0.4){
                            aaitem.alpha = 0.4;
                            clearInterval(aaitem.timer2);
                        }
                        aaitem.style.backgroundColor = " rgba(70, 192, 220,"+aaitem.alpha+")";
                    },30);
                }
              menuItems[j].selected = false;
          }
            this.selected = true;

        };
    }
    var aIframe = document.getElementById("contentIFrame");
    aIframe.onload = function () {
        setTimeout(function () {
            AnimatedObjEasyinout(aIframe,{height: aIframe.contentWindow.document.documentElement.scrollHeight});
        },500);
    };
    aIframe.src = "home.html";
};

window.onresize = iframeResize;

function iframeResize(){
    var aIframe = document.getElementById("contentIFrame");
    AnimatedObjEasyinout(aIframe,{height: aIframe.contentWindow.document.documentElement.scrollHeight});
}

function doSomeThing() {
    player.addEventListener("ended",function () {
        playButton.src = "../image/desk_play.png";
    },false);
    playWithMusicName("123.mp3");
    var hideYinliangRangeDivTimeout = null;
    var hideYinliangRangeDivTimeout2 = null;
    var yinliangDiv = document.getElementById("yinliangRangeDiv");
    yinliangDiv.onmouseout = function () {
        hideYinliangRangeDivTimeout = setTimeout(function () {
            yinliangDiv.style.visibility = "hidden";
        },500);
    };
    yinliangDiv.onmouseover = function () {
        clearTimeout(hideYinliangRangeDivTimeout);
        clearTimeout(hideYinliangRangeDivTimeout2);
    };
    document.getElementById("yinliangRange").onmouseover = function () {
        clearTimeout(hideYinliangRangeDivTimeout);
        clearTimeout(hideYinliangRangeDivTimeout2);
    };


    var luoxiaohei = document.getElementById("luoxiaohei");
    var playButton = document.getElementById("playerButton");
    var playImage = document.getElementById("playerImage");
    var stopButton = document.getElementById("stopButton");
    var yinliangButton =  document.getElementById("yinliangButton");
    stopButton.onmouseover = function () {
        stopButton.src = "../image/stop_prs.png";
    };
    stopButton.onmouseout = function () {
        stopButton.src = "../image/stop.png";
    };
    stopButton.onclick = function () {
        player.pause();
        player.currentTime = 0;
        progresRang.value = 0;
        playButton.src = "../image/desk_play.png";
        document.getElementById("timeTextDiv").innerHTML ="00:00";
    };
    yinliangButton.onmouseover = function () {
        yinliangButton.src = "../image/yinliang_prs.png";
    };
    yinliangButton.onmouseout = function () {
        yinliangButton.src = "../image/yinliang.png";
        hideYinliangRangeDivTimeout2 = setTimeout(function () {
            yinliangDiv.style.visibility = "hidden";
        },1000);
    };
    yinliangButton.onclick = function () {
        var yinliangDiv =  document.getElementById("yinliangRangeDiv");
        if(yinliangDiv.style.visibility === "visible"){
            yinliangDiv.style.visibility = "hidden";
        }else {
            yinliangDiv.style.visibility = "visible";
        }

    };
    playButton.onclick = function () {
            if (player.paused){
                player.play();
                playButton.src = "../image/desk_pause.png";
            }else {
                player.pause();
                playButton.src = "../image/desk_play.png";
            }
    };
    playButton.onmouseover = function () {
        if (player.paused){
            playButton.src = "../image/desk_play_prs.png";
        }else {
            playButton.src = "../image/desk_pause_prs.png"
        }
    };
    playButton.onmouseout = function () {
        if (player.paused){
            playButton.src = "../image/desk_play.png";
        }else {
            playButton.src = "../image/desk_pause.png";
        }
    };

    progresRang.onmousedown = function () {
      progresRang.touched = true;
    };
    progresRang.onmouseup = function () {
      progresRang.touched = false;
    };
    progresRang.onmousemove = function () {
        if (progresRang.touched){
            var minutes =  parseInt(progresRang.value/60);
            var seconds = parseInt(progresRang.value-minutes*60);
            document.getElementById("timeTextDiv").innerHTML =pad(minutes,2)+":"+pad(seconds,2);
        }

    };
    playImage.deg = 0;
    luoxiaohei.runType = 1;
    setInterval(function () {
        if (!player.paused&&!progresRang.touched){
            progresRang.max=player.duration;
            progresRang.value = player.currentTime;
            var minutes =  parseInt(player.currentTime/60);
            var seconds = parseInt(player.currentTime-minutes*60);
            document.getElementById("timeTextDiv").innerHTML =pad(minutes,2)+":"+pad(seconds,2);
            rotate(playImage,playImage.deg+2);
            playImage.deg+=2;
            if (playImage.deg>=360){
                playImage.deg =0;
             }
        }
        if (luoxiaohei.runType == 1){
            luoxiaohei.style.left = (luoxiaohei.offsetLeft - 6)+"px";
        }
        else {
            luoxiaohei.style.left = (luoxiaohei.offsetLeft - 2)+"px";
        }
        if(luoxiaohei.offsetLeft < -200){
            luoxiaohei.runType *= -1;
            if (luoxiaohei.runType == 1){
                luoxiaohei.src = "../image/luoxiaohepaobu.gif";
            }
            else {
                luoxiaohei.src = "../image/luoxiaohezoulu.gif";
            }
            luoxiaohei.style.left = document.body.offsetWidth+"px";
        }
    },30);
}


function headImageAnimation() {
    var headImg = document.getElementById("headImg");
    var leftPx = parseInt(document.body.offsetWidth/2-100);
    AnimatedObjEasyinout(headImg,{width:200,height:200,left:leftPx,top:200},function () {
        headImg.style.left = (leftPx/document.body.offsetWidth)*100+"%";
        
    });
}

function openMenu(xx) {
    if (xx){
        setTimeout(function () {
            closeMenu();
        },500);
    } 
    clearTimeout(menuTimeOut);
    AnimatedObjEasyinout(menu,{width:360});
    for (var a = 0;a<menuItems.length;a++){
        AnimatedObjEasyinout(menuItems[a],{width:150});
    }
}
function closeMenu() {
    menuTimeOut = setTimeout(function () {
        AnimatedObjEasyinout(menu,{width:50});
        for (var i = 0;i<menuItems.length;i++){
            AnimatedObjEasyinout(menuItems[i],{width:50});
        }
    },100);
    
    
}
function playWithMusicName(name) {
    if (player.paused){
        player.play();
    }else {
        player.pause();
    }
}
function playerRangeChange() {
    player.currentTime = progresRang.value;
}
function yinliangRangeChange() {
    player.volume = document.getElementById("yinliangRange").value/100;
}


